package liwei.study.jakarta.commons.beanutil;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConstructorUtils;

public class BeanUtilTest {

	public static void main(String[] args) {
		Month month = new Month(1, "Jan");

		try {
			Map map = BeanUtils.describe(month);
			Set keySet = map.keySet();
			for (Iterator iter = keySet.iterator(); iter.hasNext();) {
				Object element = (Object) iter.next();
				System.out.println("KeyClass:" + element.getClass().getName());
				System.out.println("ValueClass:"
						+ map.get(element).getClass().getName());
				System.out.print(element + "\t");
				System.out.print(map.get(element));
				System.out.println();
			}

			Month month2 = new Month();

			BeanUtils.populate(month2, map);
			Object[] argss = { new Integer(1), "Jan" };
			Class[] argsType = { int.class, String.class };
			Object obj;
			obj = ConstructorUtils.invokeExactConstructor(Month.class, argss,
					argsType);
			month = (Month) obj;

			System.out.println(BeanUtils.getProperty(month, "days"));
			System.out.println(BeanUtils.getIndexedProperty(month, "days", 1));
			System.out.println(BeanUtils.getIndexedProperty(month, "days[1]"));

		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e2) {
			e2.printStackTrace();
		} catch (NoSuchMethodException e3) {
			e3.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
	}

}
